<!DOCTYPE html>
<html>
<head>
    <title>编辑游记</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <script src="/js/plugins/jrender/jrender.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
    <link rel="stylesheet" href="/css/reset.css" />
    <link rel="stylesheet" href="/css/userTravels.css" />
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css" />
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/plugins/eleditor/Eleditor.min.js"></script>
    <script src="/js/plugins/eleditor/webuploader.min.js"></script>
    <script src="/js/plugins/My97DatePicker/WdatePicker.js"></script>
    <script src="/js/common.js"></script>
</head>

<body>
	<form id="imgForm" action="/images" method="post" enctype="multipart/form-data">
		<input type="file" name="pic" style="display: none;" accept="image/*"/>
	</form>

    <div class="search-head">
        <div class="row nav-search">
            <div class="col-2">
                <a href="javascript:window.history.go(-1);">
                    <span><i class="fa fa-times"></i></span>
                </a>
            </div>
            <div class="col-5"></div>
            <div class="col" style="text-align: right;">
                <i class="fa fa-floppy-o fg-fw" id="save"><span>保存</span></i>
                <i class="fa fa-check" id="release"><span>发布</span></i>
            </div>
        </div>
    </div>

    <div class="travels form-group">
        <div class="cover">
            <img id="coverUrl" width="100%" height="200px" render-src="coverUrl"/>
            <button type="button" class="btn" id="changeBtn">
                <i class="fa fa-pencil-square-o"></i>
                <small>更换封面</small>
            </button>
        </div>

        <div class="title">
            <input class="form-control" id="title" placeholder="请输入标题"
                   render-value="title"/>
        </div>

        <h6 href="javascript:">点击文章内容即可编辑</h6>

		<!--富文本编辑器-->
        <div id="contentEditor" render-html="travelContent.content"></div>

        <hr/>
        <div class="container">
            <form id="detailForm" method="post">
                <input type="hidden" name="id" render-value="id"/>
                <label>出行时间:</label>
                <input type="text" name="travelTime" render-value="travelTime" class="form-control"
                       onclick="WdatePicker({readOnly:true,maxDate:new Date()});">
                <label>出行天数:</label>
                <input type="number" name="days" class="form-control" render-value="days">
                <label>参与人物:</label>
                <select class="c-select form-control" name="person" render-value="person">
                    <option value="1">一个人的旅行</option>
                    <option value="2">和父母</option>
                    <option value="3">和朋友</option>
                    <option value="4">和同事</option>
                    <option value="5">和爱人</option>
                    <option value="6">和驴友</option>
                </select>
                <label>人均费用:</label>
                <input type="text" name="perExpends" class="form-control" render-value="perExpends">

                <label>旅游地点:</label>
                <select class="form-control" name="place.id" render-value="place.id"></select>

                <div class="form-check">
                    <label class="form-check-label">
                        <input class="form-check-input" type="checkbox" render-attr="checked=isPublic"
                               name="isPublic" checked>是否公开
                    </label>
                </div>
            </form>
        </div>
        <hr/>
    </div>
<script>
$(function () {
    var state; //记录当前操作的状态

    //发送ajax请求获取所有区域
    $.get("/regions", function (data) {
        $.each(data, function (index, ele) {
            //每一个迭代出来的对象做成option
            $("<option></option>").html(ele.name).val(ele.id)
                .appendTo("#detailForm select[name='place.id']");
        });
    });

    //初始化eleditor
    var edr = new Eleditor({
        el: "#contentEditor", //挂载点
        upload:{
            server: "/images",//后台接收地址
            formName: "pic"//设置文件name
        }
    });

    //选择封面
    $("#changeBtn").click(function () {
        $("#imgForm :file").click();
    });
    
    //上传封面
    $("#imgForm :file").change(function () {
        if (this.value) {
            $("#imgForm").ajaxSubmit(function (data) {
                //回填上传的图片
                $("#coverUrl").attr("src", data.url);
            });
        }
    });

    //出来保存/发布的状态值问
    $("#save").click(function () {
        state = 0;
        //提交表单
        subimtForm();
    });
    $("#release").click(function () {
        state = 1;
        //提交表单
        subimtForm();
    });
    
    function subimtForm() {
        $("#detailForm").ajaxSubmit({
            url: "/travels"+ (id ? "/"+id : ""), //处理url地址
            //往当前请求添加额外参数
            beforeSubmit: function (params) {
                //params就是当前表单的参数, 每个参数格式{name:"xx", value: xx}
                params.push({name:"state", value: state}); //添加当前状态
                params.push({name:"title", value: $("#title").val()}); //添加标题
                params.push({name:"author.id", value: getUser().id}); //添加作者
                params.push({name:"coverUrl", value: $("#coverUrl").attr("src")}); //添加封面
                params.push({name:"travelContent.content", value: edr.getContent()}); //添加文章内容
                //如果是更新操作,需要添加_method=PUT
                if (id) {
                    params.push({name:"_method", value: "PUT"});
                }
            },
            success: function (data) {
                location.href = "/mine/travels.html";
            }
        });
    }

    //获取当前需要回显的id
    var id = getParams().id;
    if (id) { //当前是编辑操作
        //发送ajax请求,获取当前游记的信息
        $.get("/travels/"+id, function (data) {
            //渲染界面
            $(".travels").renderValues(data);
        });
    }
});
</script>
</body>
</html>